package com.hcy.cinemaData;

import com.entity.Cinema;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class CinemaDateFrame extends JDialog {

    private JLabel jl_title;//标题

    private JPanel jp_north;//上部
    private JPanel jp_center;//中部
    private JPanel jp_south;//下部


    private JPanel jp_west;//左边
    private JPanel jp_east;//右边

    private JLabel jl_cName;//影院名称标签
    private JTextField jtf_cName;//影院名称文本域

    private JLabel jl_address;//地址标签
    private JTextField jtf_address;



    private JButton jbt_submit;//提交

    private CinemaFrame cinemaFrame;

    private int id;

    public CinemaDateFrame(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        this.cinemaFrame = (CinemaFrame) owner;
        this.getContentPane().setBackground(Color.WHITE);
        setLayout(new BorderLayout());


        createContent();

        jp_north =  new JPanel();
        jl_title = new JLabel(title);
        jp_north.setBackground(Color.WHITE);
        setLabel(jl_title);
        jp_north.add(jl_title);
        add(jp_north, BorderLayout.NORTH);

        jp_center =  new JPanel();
        jp_center.setLayout(new BorderLayout());

        add(jp_center, BorderLayout.CENTER);

        jp_south =  new JPanel();
        jp_south.add(jbt_submit);
        jp_south.setBackground(Color.WHITE);
        add(jp_south, BorderLayout.SOUTH);


        jp_west = new JPanel();
        jp_west.setLayout(new GridLayout(2, 1, 20, 20));
        jp_west.setPreferredSize(new Dimension(100, 0));
        jp_west.setBackground(Color.WHITE);
        jp_center.add(jp_west, BorderLayout.WEST);
        jp_west.add(jl_cName);
        jp_west.add(jl_address);

        jp_east = new JPanel();
        jp_east.setLayout(new GridLayout(2, 1, 20, 20));
        jp_east.setBackground(Color.WHITE);
        jp_center.add(jp_east, BorderLayout.CENTER);

        jp_east.add(jtf_cName);
        jp_east.add(jtf_address);

        if (this.cinemaFrame.getPnCinema()!= null) {
            Cinema cinema = this.cinemaFrame.getPnCinema();
            id = cinema.getCid();
            jtf_cName.setText(cinema.getcName());
            jtf_address.setText(cinema.getcAddress());
        }

        //1.先把窗口显示出来
        //标题
        setTitle("影院增删改查");
        //尺寸
        setSize(300,200);
        //居中
        setLocationRelativeTo(null);
        //设置关闭模式。直接结束整个程序
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //设置可见
        setVisible(true);

    }

    public void createContent() {
        jl_cName = new JLabel("电影院名称:");
        setLabel(jl_cName);
        jtf_cName = new JTextField();

        jl_address = new JLabel("地址：");
        setLabel(jl_address);
        jtf_address = new JTextField();


        jbt_submit = new JButton("提交");
        jbt_submit.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Cinema cinema = getCinema();
                System.out.println(cinema);
                int i = 0;
                if (cinemaFrame.getPnCinema() == null) {
                    //当前用户没有值，增加
                    i = cinemaFrame.getCinemaDao().insertCinema(cinema);
                } else {
                    i = cinemaFrame.getCinemaDao().updateCinema(cinema);
                }


                JOptionPane.showMessageDialog(CinemaDateFrame.this, i>0?"提交成功":"提交失败");
                //关闭自己
                dispose();
            }
        });

    }

    public Cinema getCinema() {
        String name = jtf_cName.getText();
        String address = jtf_address.getText();
        return new Cinema(id,name,address);
    }

    public void setLabel(JLabel jl) {
        //水平居中
        jl.setHorizontalAlignment(SwingConstants.RIGHT);
        //垂直居中
        jl.setVerticalAlignment(SwingConstants.CENTER);
        //设置字体
        jl.setFont(new Font("微软雅黑", Font.PLAIN, 18));

    }

    public static void main(String[] args) {
        //new CinemaDateFrame(null,"dsa",true);
    }

}